home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / ctask.zip / TSKPIP.C < prev    next >
C/C++ Source or Header  |  1988-07-01  |  6KB  |  276 lines

  1. /*
  2.    TSKPIP.C - CTask - Pipe handling routines.
  3.  
  4.    Public Domain Software written by
  5.       Thomas Wagner
  6.       Patschkauer Weg 31
  7.       D-1000 Berlin 33
  8.       West Germany
  9. */
  10.  
  11. #include <stdio.h>
  12.  
  13. #include "tsk.h"
  14. #include "tsklocal.h"
  15.  
  16. /*
  17.    tsk_getcpipe - get a byte from a pipe. For internal use only.
  18.                   Critical section assumed entered.
  19. */
  20.  
  21. local byte near tsk_getcpipe (pipeptr pip)
  22. {
  23.    byte c;
  24.  
  25.    c = pip->contents [pip->outptr++];
  26.    if (pip->outptr >= pip->bufsize)
  27.       pip->outptr = 0;
  28.    pip->filled--;
  29.    return c;
  30. }
  31.  
  32.  
  33. /*
  34.    tsk_putcpipe - put a byte to a pipe. For internal use only.
  35.                   Critical section assumed entered.
  36. */
  37.  
  38. local void near tsk_putcpipe (pipeptr pip, byte c)
  39. {
  40.    pip->contents [pip->inptr++] = c;
  41.    if (pip->inptr >= pip->bufsize)
  42.       pip->inptr = 0;
  43.    pip->filled++;
  44. }
  45.  
  46.  
  47. /* -------------------------------------------------------------------- */
  48.  
  49.  
  50. /*
  51.    create_pipe - initialises pipe.
  52. */
  53.  
  54. pipeptr far create_pipe (pipeptr pip, farptr buf, word bufsize
  55. #if (TSK_NAMEPAR)
  56.                         ,byteptr name
  57. #endif
  58.                         )
  59. {
  60. #if (TSK_DYNAMIC)
  61.    if (pip == NULL)
  62.       {
  63.       if ((pip = tsk_alloc (sizeof (pipe))) == NULL)
  64.          return NULL;
  65.       pip->flags = F_TEMP;
  66.       }
  67.    else
  68.       pip->flags = 0;
  69.    if (buf == NULL)
  70.       {
  71.       if ((buf = tsk_alloc (bufsize)) == NULL)
  72.          {
  73.          if (pip->flags & F_TEMP)
  74.             tsk_free (pip);
  75.          return NULL;
  76.          }
  77.       pip->flags |= F_STTEMP;
  78.       }
  79. #endif
  80.  
  81.    pip->wait_read = pip->wait_write = pip->wait_clear = NULL;
  82.    pip->outptr = pip->inptr = pip->filled = 0;
  83.    pip->bufsize = bufsize;
  84.    pip->contents = (byteptr)buf;
  85.  
  86. #if (TSK_NAMED)
  87.    tsk_add_name (&pip->name, name, TYP_PIPE, pip);
  88. #endif
  89.  
  90.    return pip;
  91. }
  92.  
  93.  
  94. /*
  95.    delete_pipe - kills all processes waiting for reading from or writing
  96.                  to the pipe.
  97. */
  98.  
  99. void far delete_pipe (pipeptr pip)
  100. {
  101.    CRITICAL;
  102.  
  103.    C_ENTER;
  104.    tsk_kill_queue (&(pip->wait_read));
  105.    tsk_kill_queue (&(pip->wait_write));
  106.    tsk_kill_queue (&(pip->wait_clear));
  107.    pip->outptr = pip->inptr = pip->filled = 0;
  108.    C_LEAVE;
  109.  
  110. #if (TSK_NAMED)
  111.    tsk_del_name (&pip->name);
  112. #endif
  113.  
  114. #if (TSK_DYNAMIC)
  115.    if (pip->flags & F_STTEMP)
  116.       tsk_free (pip->contents);
  117.    if (pip->flags & F_TEMP)
  118.       tsk_free (pip);
  119. #endif
  120. }
  121.  
  122.  
  123. /*
  124.    read_pipe - Wait until a character is written to the pipe. If there 
  125.                is a character in the pipe on entry, it is assigned to 
  126.                the caller, and the task continues to run. If there are
  127.                tasks waiting to write, the first task is made eligible,
  128.                and the character is inserted into the pipe.
  129. */
  130.  
  131. int far read_pipe (pipeptr pip, dword timeout)
  132. {
  133.    tcbptr curr;
  134.    int res;
  135.    CRITICAL;
  136.  
  137.    C_ENTER;
  138.  
  139.    if (pip->filled)
  140.       {
  141.       res = tsk_getcpipe (pip);
  142.  
  143.       if ((curr = pip->wait_write) != NULL)
  144.          {
  145.          tsk_putcpipe (pip, (byte)curr->retsize);
  146.          pip->wait_write = tsk_runable (curr);
  147.          curr->retptr = NULL;
  148.          }
  149.       else if (!pip->filled)
  150.          while (pip->wait_clear != NULL)
  151.             pip->wait_clear = tsk_runable (pip->wait_clear);
  152.  
  153.       C_LEAVE;
  154.       return res;
  155.       }
  156.  
  157.    tsk_wait (&pip->wait_read, timeout);
  158.    return (int)tsk_current->retptr;
  159. }
  160.  
  161.  
  162. /*
  163.    c_read_pipe - If there is a character in the pipe on entry,
  164.                  read_pipe is called, otherwise en error status is returned.
  165. */
  166.  
  167. int far c_read_pipe (pipeptr pip)
  168. {
  169.    CRITICAL, res;
  170.  
  171.    C_ENTER;
  172.    res = (pip->filled) ? read_pipe (pip, 0L) : -1;
  173.    C_LEAVE;
  174.    return res;
  175. }
  176.  
  177.  
  178.  
  179. /*
  180.    write_pipe - Wait until space for the character to be written to the 
  181.                 pipe is available. If there is enough space in the pipe 
  182.                 on entry, the character is inserted into the pipe, and
  183.                 the task continues to run. If there are tasks waiting 
  184.                 to read, the first task is made eligible, and the character
  185.                 is passed to the waiting task.
  186. */
  187.  
  188. int far write_pipe (pipeptr pip, byte ch, dword timeout)
  189. {
  190.    tcbptr curr;
  191.    CRITICAL;
  192.  
  193.    C_ENTER;
  194.  
  195.    if (pip->filled < pip->bufsize)
  196.       {
  197.       if ((curr = pip->wait_read) != NULL)
  198.          {
  199.          pip->wait_read = tsk_runable (curr);
  200.          curr->retptr = (farptr)ch;
  201.          C_LEAVE;
  202.          return 0;
  203.          }
  204.  
  205.       tsk_putcpipe (pip, ch);
  206.       C_LEAVE;
  207.       return 0;
  208.       }
  209.  
  210.    tsk_current->retsize = ch;
  211.    tsk_wait (&pip->wait_write, timeout);
  212.    return (int)tsk_current->retptr;
  213. }
  214.  
  215.  
  216. /*
  217.    c_write_pipe - If there is space for the character in the pipe on entry,
  218.                   write_pipe is called, otherwise en error status is returned.
  219. */
  220.  
  221. int far c_write_pipe (pipeptr pip, byte ch)
  222. {
  223.    int res;
  224.    CRITICAL;
  225.  
  226.    C_ENTER;
  227.    res = (pip->filled < pip->bufsize) ? write_pipe (pip, ch, 0L) : -1;
  228.    C_LEAVE;
  229.    return res;
  230. }
  231.  
  232.  
  233. /*
  234.    wait_pipe_empty - Wait until the pipe is empty. If the pipe is
  235.                      empty on entry, the task continues to run.
  236. */
  237.  
  238. int far wait_pipe_empty (pipeptr pip, dword timeout)
  239. {
  240.    CRITICAL;
  241.  
  242.    C_ENTER;
  243.    if (!pip->filled)
  244.       {
  245.       C_LEAVE;
  246.       return 0;
  247.       }
  248.  
  249.    tsk_current->retptr = NULL;
  250.    tsk_wait (&pip->wait_clear, timeout);
  251.    return (int)tsk_current->retptr;
  252. }
  253.  
  254.  
  255. /*
  256.    check_pipe - returns -1 if there are no characters in the pipe, else
  257.                 the first available character.
  258. */
  259.  
  260. int far check_pipe (pipeptr pip)
  261. {
  262.    return (pip->filled) ? (int)pip->contents [pip->outptr] : -1;
  263. }
  264.  
  265.  
  266. /*
  267.    pipe_free - returns the number of free characters in the pipe.
  268. */
  269.  
  270. word far pipe_free (pipeptr pip)
  271. {
  272.    return pip->bufsize - pip->filled;
  273. }
  274.  
  275.  
  276.